<template>
  <el-card class="m-20" shadow="hover">
    <el-button
      type="primary"
      class="el-icon-plus"
      size="medium"
      @click="dialogTableVisible = true"
    >
      添加品牌
    </el-button>
    <el-table
      class="mt-20"
      border
      ref="singleTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column type="index" width="80" label="序号" />
      <el-table-column property="tmName" label="品牌名称" width="400">
      </el-table-column>
      <el-table-column label="品牌Logo" width="400">
        <template v-slot="{ row }">
          <img :src="row.logoUrl" :alt="row.tmName" class="tmImg" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit-outline"
            size="small"
            @click="updateOneTrademark(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            @click="deleteOneTrademark(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mt-20"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[4, 8, 12, 16]"
      :page-size="pageSize"
      layout=" prev, pager, next, jumper,->, sizes, total"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      :title="`${formData.id ? '修改' : '添加'}品牌`"
      :visible.sync="dialogTableVisible"
    >
      <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
        <el-form-item label="品牌名称" prop="tmName">
          <el-input v-model="formData.tmName" />
        </el-form-item>
        <el-form-item label="品牌Logo">
          <el-upload
            class="avatar-uploader"
            :action="`${BASE_URL}/admin/product/fileUpload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="formData.logoUrl"
              :src="formData.logoUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <template #tip>
              <div class="el-upload__tip">
                只能上传jpg/png文件，且不超过200kb
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="reset">取 消</el-button>
        <el-button type="primary" @click="addOneTrademark"> 确 定 </el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getTrademark,
  addTrademark,
  delateTrademark,
  updateTrademark,
} from "@/api/trademark";
export default {
  name: "Trademark",
  data() {
    const BASE_URL = process.env.VUE_APP_BASE_API;
    return {
      dialogTableVisible: false,
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 4,
      flag: true,
      formData: {
        id: 0,
        tmName: "",
        logoUrl: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      BASE_URL,
    };
  },
  async mounted() {
    this.getTrademark();
  },
  methods: {
    async getTrademark() {
      const res = await getTrademark(this.currentPage, this.pageSize);
      // console.log(res);
      this.tableData = res.data.records;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      // console.log(val);
      this.pageSize = val;
      this.getTrademark();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTrademark();
    },
    showAddTm() {
      this.flag = false;
    },
    // 图片检验
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 < 200;

      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 500kb!");
      }
      return isJPG && isLt2M;
    },
    // 上传成功
    handleAvatarSuccess(file) {
      this.formData.logoUrl = file.data;
    },
    // 取消
    reset() {
      this.dialogTableVisible = false;
      this.formData.id = 0;
      this.formData.tmName = "";
      this.formData.logoUrl = "";
    },
    // 添加
    async addOneTrademark() {
      this.dialogTableVisible = false;
      if (this.formData.id) {
        await updateTrademark(this.formData);
        this.formData.id = 0;
        this.formData.tmName = "";
        this.formData.logoUrl = "";
      } else {
        await addTrademark(this.formData.logoUrl, this.formData.tmName);
        this.formData.tmName = "";
        this.formData.logoUrl = "";
      }
      this.$message({
        message: `${this.formData.id ? "修改" : "添加"}成功`,
        type: "success",
      });
      this.getTrademark();
    },
    // 删除
    async deleteOneTrademark(row) {
      await delateTrademark(row.id);
      this.getTrademark();
      this.$message({
        message: "删除成功",
        type: "success",
      });
    },
    // 修改
    async updateOneTrademark(row) {
      this.dialogTableVisible = true;
      this.formData.tmName = row.tmName;
      this.formData.logoUrl = row.logoUrl;
      this.formData.id = row.id;
    },
  },
};
</script>

<style scoped>
.tmImg {
  width: 80px;
  height: 50px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #000;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #c6c4c4;
}
.avatar-uploader-icon:hover {
  border-color: #409eff;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
